package thread;

public class SyncDemo4 {
    public static void main(String[] args) {
        Boo boo = new Boo();
        Thread t = new Thread(){
            @Override
            public void run() {
                boo.methodA();
            }
        };

        Thread t1 = new Thread(){
            @Override
            public void run() {
                boo.methodB();
            }
        };

        Coo c = new Coo();
        Thread t2 =new Thread(){
            @Override
            public void run() {
                c.c();
            }
        };
        Thread t3 =new Thread(){
            @Override
            public void run() {
                c.c1();
            }
        };

        t.start();
        t1.start();
        t2.start();
        t3.start();
    }
}

class Boo{
    public synchronized void methodA(){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + ":正在执行A方法...");
            Thread.sleep(2500);
            System.out.println(t.getName()+ "：执行A方法完毕。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

//    public synchronized void methodB(){
public  void methodB(){
    synchronized(this) {
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + ":正在执行B方法...");
            Thread.sleep(2500);
            System.out.println(t.getName() + "：执行B方法完毕。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    }
}

class Coo {
    public synchronized void  c (){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + "正在执行c");
            Thread.sleep(2500);
            System.out.println(t.getName()+"c执行结束。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void  c1 (){
        try {
            Thread t = Thread.currentThread();
            System.out.println(t.getName() + "正在执行c");
            Thread.sleep(2500);
            System.out.println(t.getName()+"c执行结束。");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}


